<?php  
class SystemController extends BaseAuthController {
    public function init() {
        parent::init();
    }
    //如果需要新增其他缓存更新，请新增数组里面的元素
    public $cacheType = array(
        1 =>array(
            'key' => 'exp_pc_index_nav_cache*' , 
            'name' => 'cwLive的城市导航数据缓存（PC:1小时）' ,
            'cache_type' => "redis" , 
            'redis_db' => 6 , 
        ),
        2 =>array(
            'key' => 'exp_pc_item_detail_cache*' ,
            'name' => 'cwLive的项目详情缓存（PC：1小时）',
            'cache_type' => "redis",
            'redis_db' => 6 , 
        ),  //可以带*号
        3 =>array(
            'key' => 'exp_pc_index_ad_banner*' ,
            'name' => 'cwLive首页banner广告缓存（PC：1小时）' ,
            'cache_type' => "redis",
            'redis_db' => 6 , 
        ),  //可以带*号
    );

    public function cacheAction(){
        if(!in_array($this->userInfo['username'] , array('pengjun@shixiba.com','wangjian@speakphp.com','yangbo@shixiba.com') )){
            Common::EchoResult(0,"权限不够，此项操作只有杨波和研发哥哥有权操作,请找产品经理杨波清理缓存");
        }
        $action = $this->getRequest()->getPost("action");
        $action_array = array("show","doCache");
        $action = !in_array($action,$action_array) ? 'show' : $action ;
        if($action == 'show' ){
           $this->displayTemplate("cache/index" , array(
                'cache' => $this->cacheType,
                'action' => "/system/cache"//提交地址
            ) );
        }elseif($action == 'doCache'){
            $cache = isset($_POST['cache']) ? $_POST['cache'] : '';
            if(!$cache or empty($cache)){
                $this->error("请选择更新" , "/system/cache");
            }
            if(!array_key_exists($cache, $this->cacheType)){
                $this->error("参数错误" , "/system/cache");
            }
            switch ($cache) {
                case '5':
                    $this->delFolder($cache);
                    break;
                case '6':
                    $this->delFolder($cache);
                    break;
                default:
                    $redisDB = $this->cacheType[$cache]['redis_db'];
                    $object = RedisDB::getInstance($redisDB)->handler ;
                    $keys = $this->cacheType[$cache]['key'];
                    if( stripos($keys , "*" ) !== false ){
                        $keys = $object->keys($keys);
                    }
                    $object->del($keys);
                    break;
            }
            header("Location:/system/cache");
        }
    }
}

